package com.yvdedu.controller;




import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yvdedu.entity.User;
import com.yvdedu.service.UserService;

@Controller
public class Login_Controller {

	private UserService us = new UserService();

	@RequestMapping("/gogo")
	public String handler() {

		return "login";
	}

	@RequestMapping("/login")
	@ResponseBody
	public String handler(HttpServletRequest request, HttpSession session) {
		String userName = request.getParameter("userName");
		String pwd = request.getParameter("pwd");
		String yzm = request.getParameter("yzm");
		String code = (String) session.getAttribute("rand");
//		System.out.println(code);
//		System.out.println(userName);
//		System.out.println(pwd);
		if (!yzm.equalsIgnoreCase(code)) {
			return "codeErr";
		} else if (!"".equals(userName) && !"".equals(pwd)) {
			User u = us.login(userName, pwd);
			if (u != null) {
				session.setAttribute("user", u);
				return "success";
			} else {
				return "false";
			}
		} else {
			return null;
		}
	}

	@RequestMapping("/welcome")
	public String welcom() {
		return "welcome";
	}

	@RequestMapping("/exit") 
	public String exitssss(HttpServletRequest request) {
		request.getSession().removeAttribute("user");
		return "login";
		
	}

}
